package scfw.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import scfw.model.comm.JsonBean;
import scfw.service.FoodServiceI;
import scfw.service.model.FoodSrModel;
import scfw.service.model.UserSrModel;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping({"/foodController"})
public class FoodController
{
	private static final Logger logger = LoggerFactory.getLogger(FoodController.class);

	@Autowired
	private FoodServiceI foodService;

	@RequestMapping({"/queryFoods"})
	public String queryFoods(HttpServletRequest req) {
		try {
			UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
			if (user != null) {
				List list = this.foodService.queryFoods(user.getId());
				req.setAttribute("foods", list);
			} else {
				logger.info("用户还未登陆系统……");
				return "redirect:/userController/login.html";
			}
		}
		catch (Exception localException) {

		}
		return "food/queryFoods";
	}

	@RequestMapping({"/todayOrderItems"})
	public String todayOrderItems(HttpServletRequest req) {
		UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
		if (user != null) {
			List list = this.foodService.todayOrderItems(user.getId());
			req.setAttribute("orderItems", list);
			return "food/todayOrderItems";
		}
		logger.info("用户还未登陆系统……");
		return "redirect:/userController/login.html";
	}

	@RequestMapping({"/historyOrderItems"})
	public String historyOrderItems(HttpServletRequest req)
	{
		UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
		if (user != null) {
			List list = this.foodService.historyOrderItems(user.getId());
			req.setAttribute("orderItems", list);
			return "food/historyOrderItems";
		}
		logger.info("用户还未登陆系统……");
		return "redirect:/userController/login.html";
	}

	@RequestMapping({"/todayAllOrderItems"})
	public String todayAllOrderItems(HttpServletRequest req)
	{
		UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
		if (user != null) {
			List list = this.foodService.todayAllOrderItems();
			req.setAttribute("userOrderItems", list);
			return "food/todayAllOrderItems";
		}
		logger.info("用户还未登陆系统……");
		return "redirect:/userController/login.html";
	}

	@RequestMapping({"/historyAllOrderItems"})
	public String historyAllOrderItems(HttpServletRequest req)
	{
		UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
		if (user != null) {
			List list = this.foodService.historyAllOrderItems();
			req.setAttribute("userOrderItems", list);
			return "food/todayAllOrderItems";
		}
		logger.info("用户还未登陆系统……");
		return "redirect:/userController/login.html";
	}

	@RequestMapping({"/submitFoods"})
	@ResponseBody
	public JsonBean submitFoods(HttpServletRequest req)
	{
		JsonBean json = new JsonBean();
		try {
			FoodSrModel srModel = new FoodSrModel();
			UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
			srModel.setUser(user);
			String[] foodIds = req.getParameterValues("foodIds");
			srModel.setFoodIds(foodIds);
			if (user != null) {
				this.foodService.submitFoodOrder(srModel);
				json.setIsSuccess(1);
			} else {
				json.setIsSuccess(0);
				json.setMsgInfo("请先登录系统");
			}
		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping({"/flushBanlance"})
	@ResponseBody
	public JsonBean flushBanlance(HttpServletRequest req)
	{
		JsonBean json = new JsonBean();
		try {
			UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
			json.addObject("myBanlance", user.getMyBanlance());
		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping({"/{oid}/{fid}/cancelOrderItem"})
	@ResponseBody
	public JsonBean cancelOrderItem(@PathVariable int oid, @PathVariable int fid, HttpServletRequest req)
	{
		JsonBean json = new JsonBean();
		try {
			UserSrModel user = (UserSrModel)req.getSession().getAttribute("user");
			this.foodService.cancelOrderItem(oid, fid, user);
			json.setIsSuccess(1);
		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping({"/submitOrderItems"})
	@ResponseBody
	public JsonBean submitOrderItems(HttpServletRequest req)
	{
		JsonBean json = new JsonBean();
		try {
			this.foodService.submitOrderItems();
			json.setIsSuccess(1);
		} catch (Exception e) {
			json.setIsSuccess(0);
			json.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
}